<?php

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;
use think\Request;

class DriverRefuseOrder extends Controller
{
    use \app\admin\traits\controller\Controller;
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $map['df.isdelete'] = ['<>',1];//删除
        $data = input();

        if(!empty($data['start_time']) && !empty($data['end_time'])){
            $strat_time = $data['start_time']." 0:0:0";
            $strat_time = strtotime($strat_time);
            $end_time = $data['end_time']." 23:59:59";
            $end_time = strtotime($end_time);
            $map['df.create_time'] = ['between',[$strat_time,$end_time]];
        }elseif (!empty($data['start_time'])){
            $strat_time = $data['start_time']." 0:0:0";
            $strat_time = strtotime($strat_time);
            $map['df.create_time'] = ['>',$strat_time];
        }elseif (!empty($data['end_time'])){
            $end_time = $data['end_time']." 23:59:59";
            $end_time = strtotime($end_time);
            $map['df.create_time'] = ['<',$end_time];
        }
        if ($data['user_id']){
            $adminUser = Db::name('admin_user')->where('realname','like','%'.$data['user_id'].'%')->field('id,realname')->select();
            $adminUserIds = '';
            foreach ($adminUser as $auk=>$auv){
                $adminUserIds .= $auv['id'].',';
            }
            $adminUserIds = rtrim($adminUserIds,',');
            $map['df.user_id'] = ['in',$adminUserIds];
        }
        $driver_refused = Db::name('driver_refused')
                            ->alias('df')
                            ->join('tp_admin_user au','au.id=df.user_id','left')
                            ->where($map)
                            ->field('df.*,au.realname')
                            ->order('id','desc')
                            ->paginate(20);
        $total = $driver_refused->total();
        return view('',compact('driver_refused','total'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id = input('id');
        $id = explode(',',$id);
        foreach ($id as $k=>$v){
            $up = Db::name('driver_refused')->where('id',$v)->update(['isdelete'=>1]);
        }
        if ($up){
            return ajax_return_adv('移到回收站成功');
        }else{
            return ajax_return_adv_error('移到回收站失败');
        }
    }

    /**
     * 查询移到回收站的数据
     *
     */
    public function recyclebin()
    {
        $data = input();

        if(!empty($data['start_time']) && !empty($data['end_time'])){
            $strat_time = $data['start_time']." 0:0:0";
            $strat_time = strtotime($strat_time);
            $end_time = $data['end_time']." 23:59:59";
            $end_time = strtotime($end_time);
            $map['df.create_time'] = ['between',[$strat_time,$end_time]];
        }elseif (!empty($data['start_time'])){
            $strat_time = $data['start_time']." 0:0:0";
            $strat_time = strtotime($strat_time);
            $map['df.create_time'] = ['>',$strat_time];
        }elseif (!empty($data['end_time'])){
            $end_time = $data['end_time']." 23:59:59";
            $end_time = strtotime($end_time);
            $map['df.create_time'] = ['<',$end_time];
        }
        if ($data['user_id']){
            $adminUser = Db::name('admin_user')->where('realname','like','%'.$data['user_id'].'%')->field('id,realname')->select();
            $adminUserIds = '';
            foreach ($adminUser as $auk=>$auv){
                $adminUserIds .= $auv['id'].',';
            }
            $adminUserIds = rtrim($adminUserIds,',');
            $map['df.user_id'] = ['in',$adminUserIds];
        }
        $list = Db::name('driver_refused')
            ->alias('df')
            ->join('tp_admin_user au','au.id=df.user_id','left')
            ->where('df.isdelete','<>',0)
            ->field('df.*,au.realname')
            ->paginate(20);
        $count = $list->total();
        return view('recyclebin',compact('list','count'));
    }

    /**
     * 从回收站恢复数据
     *
     */
    public function recycle()
    {
        $id = input('id');
        $id = explode(',',$id);
        foreach ($id as $k=>$v){
            $up = Db::name('driver_refused')->where('id',$v)->update(['isdelete'=>0]);
        }
        if ($up){
            return ajax_return_adv('恢复成功!');
        }else{
            return ajax_return_adv_error('恢复失败!');
        }
    }

    /**
     * 彻底删除数据
     *
     */
    public function deleteforever()
    {
        $id = input('id');
        $id = explode(',',$id);
        foreach ($id as $k=>$v){
            $up = Db::name('driver_refused')->delete($id);
        }
        if ($up){
            return ajax_return_adv('删除数据成功!');
        }else{
            return ajax_return_adv_error('删除数据失败!');
        }
    }

    /**
     * 清空回收站
     */
    public function clear()
    {
        $del = Db::name('driver_refused')->where('isdelete',1)->delete();
        if ($del){
            return ajax_return_adv('清空回收站成功!');
        }else{
            return ajax_return_adv_error('清空回收站失败');
        }
    }

    /**司机详细资料**/
    public function details(Request $request)
    {
        $data = input();
        $vo = Db::name('driver')->where('user_id', $data['user_id'])->find();
        if (!$vo){
            return ajax_return_adv_error('不存在该司机信息');
        }
        $users = Db::name('admin_user')->where('id',$vo['user_id'])->find();
        if ($users['id']){
            $users['user_id'] = $users['id'];
            unset($users['id']);
        }
        if (!$users){
            return ajax_return_adv_error('不存在该司机的用户信息',1);
        }
        //查询司机等级
        $driverRank = Db::name('driver_rank')->where('id',$vo['driver_rank_id'])->find();
        if ($driverRank){
            $vo['driver_rank_id'] = $driverRank['name'];
        }else{
            $vo['driver_rank_id'] = '新手';
        }
        $this->view->assign('users', $users);
        $this->view->assign('vo', $vo);
        return $this->view->fetch('details');
    }
}
